<?php
/*
 * 主要功能:
 *
 * dreamxyp(QQ:31996798) - PHP - class.main.php
 * coding:夏一平
 * 創建時間:2006-11-6
 */
class admin_main
{
	/*
	 * 功能:歡迎頁面
	 */
	static public function welcome()
	{
		global $menu;
		$str = '<table class="tableoutline" cellSpacing="1" cellPadding="4" width="400" align="center" border="0">
				  <tr class="option_sort">
					<td height="25"><b>我 的 權 限</b></td>
				  </tr>';
	  	foreach($menu['field'] as $k => $v)
		{
			$purview =$v['key']?self::chk($v['key']):true;
			if($purview)
			{
			   $str .='<tr class="firstalt">
						<td> &gt;&gt; '.$v['note'].' ok!</td>
						</tr>';
			}
		}
		if($_SESSION[$menu['adminStr'].'admin_q_note'])
		{
			$str .='<tr class="secondalt">
					<td>管理員備註:
					  <textarea name="textarea" cols="60" rows="4">'.$_SESSION[$menu['adminStr'].'admin_q_note'].'</textarea></td>
					</tr>';
		}
		$str .='</table>';
		return $str;
	}
	/*
	 * 功能:生成菜單
	 */
	static public function menu()
	{
		global $menu;
		$reFiles ='<script language="javascript">
					<!--
					function ToggleNode(nodeObject, imgObject)
					{
							 if (nodeObject.style.display == "" || nodeObject.style.display == "inline")
							 {
								 nodeObject.style.display = "none";
								 //$(nodeObject).hide("slow");
								 imgObject.src = "img/collapse.gif";
							 } else
							 {
								 nodeObject.style.display = "inline";
								 //$(nodeObject).show("slow");
								 imgObject.src = "img/expand.gif";
							 }
					}
					-->
					</script>
					<table cellSpacing="1" cellPadding="1" width="100%" border=0>';
		foreach($menu['field'] as $k => $v)
		{
			$purview =$v['key']?self::chk($v['key']):true;
			if($purview)
			{
			   $reFiles .='<tr class="tbnav" style="CURSOR: hand" onclick="ToggleNode(nav_tr_'.$k.',nav_img_'.$k.')">
							<td><img id="nav_img_'.$k.'" src="img/expand.gif" align="absMiddle">'.$v['note'].'</td>
						  </tr>
						  <tr id="nav_tr_'.$k.'">
							<td>
								<table cellSpacing="1" cellPadding="0" width="100%" border="0">';
				foreach($v['subArray'] as $key => $value)
				{
					$reFiles.='<tr class=firstalt onMouseOver="this.className=\'secondalt\'"  onmouseout="this.className=\'firstalt\'">
								<td>&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$key.'" target="content">'.$value.'</a> </td>
							   </tr>';
				}
				$reFiles.='</table></td></tr>';
			}
		}
		$reFiles .='</table>';
		return $reFiles;
	}
	/*
	 * 功能:admin登錄 CHK
	 */
	static public function login($id,$pwd,$table="flv_master",$acconutField="master",$pwdField="password")//帳號，密碼，表，帳號字段，密碼字段
	{
		global $db_admin;
		$rsArray=$db_admin->rsArray("select `".$pwdField."` from `".$table."` where `".$acconutField."`=? ",$id);
		if($rsArray)
		{
			if($rsArray[$pwdField]==self::mixPass($pwd))
			{
				return true;
			}else
			{
				return false;
			}
		}else
		{
			return false;
		}
	}
	/*
	 * 功能:頁面權限檢測
	 */
	static public function pageChk($a=false)
	{
		global $menu;
		if($a)
		{
			$_SESSION[$menu['adminStr'].'adminChk'] = $a;
		}else
		{
			$_SESSION[$menu['adminStr'].'adminChk'] = '__chk__false__';
		}
		if(!self::chk($a))
		{
			//寫日誌
			#..
			echo g::msg('錯誤!對不起，你沒有足夠的權限操作![code:001]');
			g::gowin('s_main.php');
			exit();
		}
	}
	/*
	 * 功能:操作權限檢測
	 */
	static public function actionChk($action='')
	{
		global $menu;
		if(admin_main::chk($_SESSION[$menu['adminStr'].'adminChk']) or $_SESSION[$menu['adminStr'].'adminChk'] == '__chk__false__')
		{
			//寫日誌
			#..
		}else
		{
			//寫日誌
			#..
			echo g::msg('錯誤!對不起，你沒有足夠的權限操作![code:002]');
			g::gowin("s_main.php");
			exit();
		}
	}
	/**
	 * 功能：得到管理員帳號
	*/
	static public function getMaster()
	{
		global $menu;
		return $_SESSION[$menu['adminStr']];
	}
	/*
	 * 功能:權限檢測
	 */
	static public function chk($rows,$admin='')
	{
		global $db_admin,$menu;
		if(empty($rows)) return true;
		//
		if(empty($admin))
		{
			$admin	=$_SESSION[$menu['adminStr']];
			if(empty($_SESSION['admin_qx'])){
				$rsArray=$db_admin->rsArray("select `id`,`".$menu['masterField']."`,`".$menu['password']."`,`".$menu['keyField']."`,`".$menu['noteField']."`  from `".$menu['table']."`  where `".$menu['masterField']."`= ? limit 1;",$admin);
				$_SESSION[$menu['adminStr'].'admin_qx']	 =$rsArray[$menu['keyField']];
				$_SESSION[$menu['adminStr'].'admin_q_note']=$rsArray[$menu['noteField']];
			}
			$chk=explode(',',$_SESSION[$menu['adminStr'].'admin_qx']);
		}else
		{
			if(empty($_SESSION['admin_qx_'.$admin])){
				$rsArray=$db_admin->rsArray("select `id`,`".$menu['masterField']."`,`".$menu['password']."`,`".$menu['keyField']."`,`".$menu['noteField']."`  from `".$menu['table']."`  where `".$menu['masterField']."`= ? limit 1;",$admin);
				$_SESSION[$menu['adminStr'].'admin_qx_'.$admin]	=$rsArray[$menu['keyField']];
			}
			$chk=explode(',',$_SESSION[$menu['adminStr'].'admin_qx_'.$admin]);
		}
		$rows = explode(",",$rows);
		foreach($rows as $v)
		{
			if(in_array($v,$chk))
			{
				return true;
			}
		}
		return false;
	}
	/*
	 * MD5加密
	 * 參數:$makePassStr
	 * 返回:密文
	 */
	static public function mixPass($str)
	{
		return substr(md5($str),5,16);
	}
}
?>